home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / Libraries / PlayerPRO 4.5.8 / PlayerPRO 4.5.8 Dev.Kit / MADH Library 4.5 / MacOS Examples / AEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-10  |  4.2 KB  |  181 lines  |  [TEXT/CWIE]

  1. #include "Retrace.h"
  2. #include "Start.h"
  3. #include "Gestalt.h"
  4. #include "MAD.h"
  5. #include "RDriver.h"
  6. #include "appleevents.h"
  7.  
  8. extern     Boolean     OnTourne;
  9. extern    DialogPtr    myDialog;
  10.  
  11. OSErr MyGotRequiredParams (AppleEvent *theAppleEvent);
  12. pascal OSErr  MyHandleODoc (AppleEvent *theAppleEvent, AppleEvent* reply, long handlerRefCon);
  13. pascal OSErr  MyHandleQDoc (AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefCon);
  14. pascal OSErr  MyHandleQApp (AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefCon);
  15. /********************************/
  16. /********************************/
  17.  
  18. extern MADDriverRec    *MADDriver;
  19.  
  20. void OSType2Str( OSType type, Str255 str)
  21. {
  22.     short i;
  23.  
  24.     str[ 0] = 4;
  25.     BlockMove( &type, str+1, 4);
  26.  
  27.     for( i = 4; i > 0; i--)
  28.     {
  29.         if( str[ i] == ' ') str[ 0]--;
  30.         else return;
  31.     }
  32. }
  33.  
  34. void OSType2Ptr( OSType type, Ptr str)
  35. {
  36.     BlockMove( &type, str, 4);
  37.     str[ 4] = 0;
  38. }
  39.  
  40. Boolean AppleEventsInstalled (void)
  41. {
  42.     OSErr err;
  43.     long  result;
  44.  
  45.     err = Gestalt (gestaltAppleEventsAttr, &result);
  46.     return (!err && ((result >> gestaltAppleEventsPresent) & 0x0001));
  47. }
  48.  
  49. pascal OSErr  MyHandleQApp (AppleEvent *theAppleEvent, AppleEvent *reply, long
  50.                                                         handlerRefCon)
  51. {
  52.     AEDescList    docList;
  53.     OSErr        err;
  54.     long        itemsInList;
  55.  
  56.     OnTourne = false;
  57.     
  58.     // get the direct parameter--a descriptor list--and put it into a docList
  59.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList,
  60.                                     &docList);
  61.     if (err) return err;
  62.  
  63.     // check for missing parameters
  64.     err = MyGotRequiredParams (theAppleEvent);
  65.     if (err) return err;
  66.  
  67.     // count the number of descriptor records in the list
  68.     err = AECountItems (&docList, &itemsInList);
  69.  
  70.     err = AEDisposeDesc (&docList);
  71.     return noErr;
  72. }
  73.  
  74. OSErr MyGotRequiredParams (AppleEvent *theAppleEvent)
  75. {
  76.     DescType    returnedType;
  77.     Size    actualSize;
  78.     OSErr    err;
  79.  
  80.     err = AEGetAttributePtr (theAppleEvent, keyMissedKeywordAttr,
  81.                                     typeWildCard, &returnedType, nil, 0,
  82.                                     &actualSize);
  83.     if (err == errAEDescNotFound)    // you got all the required parameters
  84.             return noErr;
  85.     else if (!err)            // you missed a required parameter
  86.             return errAEEventNotHandled;
  87.     else                    // the call to AEGetAttributePtr failed
  88.             return err;
  89. }
  90.  
  91. void ReadAndPlayMusic( Str255    myFile, OSType    fdType)
  92. {
  93.     char str[ 5];
  94.     
  95.     OSType2Ptr( fdType, str);
  96.     
  97.     if( !MADPlugAvailable( str)) return;
  98.  
  99.     MADStopDriver();
  100.     MADDisposeMusic();
  101.  
  102.     if( MADPlugAvailable( str))        // Is available a plug to open this file?
  103.     {
  104.         MADLoadMusicFilePString( str, myFile);
  105.     }
  106.     
  107.     if( MADDriver->header->numChn != MADDriver->DriverSettings.numChn)
  108.     {
  109.         MADChangeTracks( MADDriver->header->numChn);
  110.     }
  111.     
  112.     SetWTitle( myDialog, myFile);
  113.  
  114.     MADStartDriver();
  115.     MADDriver->Reading = true;
  116. }
  117.  
  118. pascal OSErr  MyHandleODoc (AppleEvent *theAppleEvent, AppleEvent* reply, long
  119.                                                         handlerRefCon)
  120. {
  121.     FSSpec            myFSS;
  122.     AEDescList        docList;
  123.     OSErr            err;
  124.     long            index, itemsInList;
  125.     Size            actualSize;
  126.     AEKeyword        keywd;
  127.     DescType        returnedType;
  128.     FInfo            fndrInfo;
  129.     
  130.     // get the direct parameter--a descriptor list--and put it into a docList
  131.     
  132.     err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList,
  133.             &docList);
  134.     if (err)
  135.             return err;
  136.  
  137.     // check for missing parameters
  138.     err = MyGotRequiredParams (theAppleEvent);
  139.     if (err)
  140.             return err;
  141.  
  142.     // count the number of descriptor records in the list
  143.     err = AECountItems (&docList, &itemsInList);
  144.  
  145.     // now get each descriptor record from the list, coerce the returned
  146.     // data to an FSSpec record, and open the associated file
  147.     
  148.     for (index = 1; index <= itemsInList; index++)
  149.     {
  150.         err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
  151.                         &returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
  152.         
  153.         err = HGetFInfo( myFSS.vRefNum, myFSS.parID, myFSS.name, &fndrInfo);
  154.         
  155.         err = HSetVol( 0L, myFSS.vRefNum, myFSS.parID);
  156.  
  157.         if( err == noErr)
  158.         {
  159.             ReadAndPlayMusic( myFSS.name, fndrInfo.fdType);
  160.         }
  161.     }
  162.     
  163.     err = AEDisposeDesc (&docList);
  164.     return noErr;
  165. }
  166.  
  167. short InstallAE(void)
  168. {
  169.     Boolean        aEvents;
  170.     OSErr        err;
  171.  
  172.     aEvents = AppleEventsInstalled();
  173.     
  174.     if (aEvents)
  175.     {
  176.         err = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerProc( MyHandleODoc),0, false);
  177.         err = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc( MyHandleQApp),0, false);
  178.     }
  179.  
  180.     return 0;
  181. }